<?php

//扫码登录
include './sdk/We.class.php';
include './sdk/jssdk.php';
$jssdk = new JSSDK();
$signPackage = $jssdk->GetSignPackage();
$we = new We();
$at = isset($_GET['at'])?$_GET['at']:'index';
if($at=='index'){

	if(empty($_GET['url'])){
	//	echo 'no url';exit;
	}
	$url = "http://api.mtaction.com/dian/Qm/carve/index.php?at=sq";
	//获取微信授权链接
	$wxurl = $we->get_authorize_url($url,1);
header('Location:'.$wxurl);
}else if($at=='sq'){

if($_GET['code']){
$code = $_GET['code'];
//授权获取

$res = $we->get_access_token($code);

if($res===false && empty($res['access_token'])){
echo '授权失败';exit;
}else{
if(empty($res['access_token'])){
header('Location: http://api.mtaction.com/dian/Qm/carve/index.php?at=index');exit;
}

//获取用户基本信息
$res_user = $we->get_user_info($res['access_token'],$res['scope']);
//记录session
//	setcookie('access_token',$res['access_token'],time()+60*60*1000);
setcookie('openid',$res_user['openid'],time()+10*60*1000);
}
//header('Location: http://api.mtaction.com/dian/huaxing/index.html');
}else{
echo 'No Code';exit;
}

}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登陆成功</title>
    <meta name="keywords" content="">
    <meta name="Description" content="">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />

    <?php
print_r([
            'openid'=>$res_user['openid'],
            'nickname'=>$res_user['nickname'],
            'avatar'=>$res_user['headimgurl']]
);
?>